﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;
using Thrift;

namespace Me.Prettyprint.Cassandra.Serializers
{
    /// <summary>
    /// By default the row count is set to 100
    /// </summary>
    /// <typeparam name="K"></typeparam>
    public class HKeyRange<K>
    {
        public HKeyRange()
        {
            RowCount = 100;
        }
        public HKeyRange(Serializer<K> keySerializer) : this()
        {
            if (keySerializer == null)
            {
                throw new NullReferenceException("KeySerializer cannot be null");
            }
            KeySerializer = keySerializer;
        }
        public K StartKey
        {
            get;set;
        }
        public K EndKey
        {
            get;set;
        }
        public string StartToken
        {
            get;
            set;
        }
        public string EndToken
        {
            get;set;
        }
        public List<IndexExpression> RowFilters
        {
            get;set;
        }
        public int RowCount
        {
            get;
            set;
        }
        private Serializer<K> KeySerializer
        {
            get;
            set;
        }
        public HKeyRange<K> SetKeys(K start, K end)
        {
            StartKey = start;
            EndKey = end;
            StartToken = null;
            EndToken = null;
            return this;
        }
        public HKeyRange<K> setTokens(K startKey, string startToken, string endToken)
        {
            StartKey = startKey;
            EndKey = default(K);
            StartToken = startToken;
            EndToken = endToken;
            return this;
        }
        public void AddToExpressions(IndexExpression elem)
        {
            if (RowFilters == null)
            {
                RowFilters = new List<IndexExpression>();
            }
            RowFilters.Add(elem);
        }
        public KeyRange ToThrift()
        {
            KeyRange keyRange = new KeyRange(RowCount);
            keyRange.Row_filter = new List<IndexExpression>();
            if(StartToken != null)
            {
                keyRange.Start_token = StartToken;
                keyRange.__isset.start_token = true;
            }
            else
            {
                keyRange.Start_key = StartKey == null ? new byte[0] : KeySerializer.ToBytes(StartKey);
                keyRange.__isset.start_key = true;
            }
            if(EndToken != null)
            {
                keyRange.End_token = EndToken;
                keyRange.__isset.end_token = true;
            }
            else
            {
                keyRange.End_key = (EndKey == null ? new byte[0] : KeySerializer.ToBytes(EndKey));
                keyRange.__isset.end_key = true;
            }
            if (RowFilters != null)
            {
                foreach (IndexExpression filter in RowFilters)
                {
                    keyRange.Row_filter.Add(filter);
                }
            }
            return keyRange;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("HKeyRange(start:");
            sb.Append(StartKey);
            sb.Append(",end:");
            sb.Append(EndKey);
            sb.Append(",indexed expressions:");
            if (RowFilters == null || RowFilters.Count == 0)
            {
                sb.Append("[]");
            }
            else
            {
                sb.Append("[");
                int i = 0;
                foreach (IndexExpression expr in RowFilters)
                {
                    if (i > 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(expr.ToString());
                    i++;
                }
                sb.Append("]");
                sb.Append(")");
            }
            return sb.ToString();
        }

    }
}
